#!/usr/bin/python3
# @Time    : 2018/6/3 上午 7:35:12  
# @Author  : LaoJI
import datetime

from sqlalchemy import Column, ForeignKey, func
from sqlalchemy import String, Unicode, DateTime, Boolean
from sqlalchemy import SmallInteger, Integer, TIMESTAMP
from sqlalchemy.orm import relationship
from app.models.base import db, Base


class MyClass(Base):
    __tablename__ = 'classes'

    class_id = Column("class_id", Integer, primary_key=True)
    class_name = Column("class_name", String(255), default="whatclass", nullable=False)
    invite_code = Column("invite_code", String(20), default="111111", nullable=False)
    class_descri = Column("class_descri", String(255), default="暂时没有描述哦", nullable=False)
    student_nums = Column("student_nums", Integer, nullable=False, default=0)
    exam_nums = Column("exam_nums", Integer, nullable=False, default=0)
    notice_num = Column("notice_num", Integer, nullable=False, default=0)
    class_logo = Column("class_logo", String(255), nullable=False, default=None)
    class_color = Column("class_color", String(255), nullable=False, default=None)
    created_at = Column("created_at", TIMESTAMP, default=datetime.datetime.now(), nullable=False)
    updated_at = Column("updated_at", TIMESTAMP, default=datetime.datetime.now(), nullable=False)

    teacher_id = Column("teacher_id", Integer, ForeignKey('users.id'), nullable=False, )
    Teacher = relationship('Teacher', backref=db.backref('classes', lazy='dynamic'))

    @staticmethod
    def get_class_by_code(code):
        myclass = MyClass.query.filter_by(invite_code=code).first()
        return myclass

    @staticmethod
    def addClass(cid, uid):
        # res = db.session.execute('select * from stu_class')
        # fetchall = res.fetchall()
        # for ec in fetchall:
        #     print(ec)
        res = db.session.execute(
            "select * from stu_class where student_student_id  = :sid and class_class_id = :cid",
            {"sid": uid, "cid": cid}).fetchall()
        if len(res) == 1:
            return False
        execute = db.session.execute(
            "INSERT INTO stu_class (student_student_id, class_class_id) VALUES ({0}, {1})".format(int(uid), int(cid)))
        db.session.commit()
        # if execute.rowcount == 1:
        #     return True
        return True

    @staticmethod
    def getAllClass(uid):
        result = []
        res = db.session.execute(
            "select * from stu_class where student_student_id  = :sid",
            {"sid": uid}).fetchall()
        exam_class = [{
            'student_student_id': es['class_class_id'] or '',
            'class_class_id': es['class_class_id'] or ''
        } for es in res]
        for es in exam_class:
            cls_tc_msg = {}
            cid = es["class_class_id"]
            myclass = MyClass.query.get(cid)
            tname = myclass.Teacher.nick_name
            cls_tc_msg["tname"] = tname
            cls_tc_msg["class"] = myclass
            result.append(cls_tc_msg)
        return result
